<?php

/**
 * agenda actions.
 *
 * @package    fisio-ud
 * @subpackage agenda
 * @author     Johan AR <j3valentin@gmail.com>
 */
class agendaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $criteria = new Criteria();
    $criteria->add(SProfesionalAgendaPeer::USUARIO_ID, $this->getUser()->getProfile()->getId());
    $this->SProfesionalAgendas = SProfesionalAgendaPeer::doSelectJoinAll($criteria);
    
    $this->getResponse()->addStyleSheet('/fullcalendar/fullcalendar.css');
    $this->getResponse()->addJavaScript('/fullcalendar/fullcalendar.min.js');
  }
  
  public function executeIndex2(sfWebRequest $request)
  {
    error_reporting(E_ALL);
    ini_set('display_errors', '1');

    $this->Usuario = $this->getUser()->getProfile();
    $this->Usuario->getSAgendaNovedads();
    $this->Usuario->getActividads();
    $this->Usuario->getSCitasRelatedByProfesionalId();
    
  }
  
  public function executeIndex3(sfWebRequest $request)
  {
//    $this->SProfesionalAgendas = SProfesionalAgendaQuery::create()->find();
    $criteria = new Criteria();
    $criteria->add(SProfesionalAgendaPeer::USUARIO_ID, $this->getUser()->getProfile()->getId());
    //$this->SProfesionalAgendas = SProfesionalAgendaPeer::doSelectJoinAll($criteria);
    $this->SProfesionalAgendas = SProfesionalAgendaPeer::doSelectJoinAll($criteria);
    
    if ($request->hasParameter('fecha')) {
      $this->lunes = date('d',mktime(0, 0, 0, date("m", $request->getParameter('fecha'))  , date("d", $request->getParameter('fecha'))));
    }
    else
      $this->lunes = date('d',mktime(0, 0, 0, date("m")  , date("d")-date('N')+1, date("Y")));
    
    $this->dias = FtUsuarioAsistenciaPeer::getDays();
    $this->dias[0] = "Hora";
    $this->rango = 20;
    $this->turnosHora = 60 / $this->rango;
    $this->inicio = 8;
    $final = 17;
    $this->turnosDia = ($final - $this->inicio) * $this->turnosHora;
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->SProfesionalAgenda = SProfesionalAgendaPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->SProfesionalAgenda);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new SProfesionalAgendaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new SProfesionalAgendaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $SProfesionalAgenda = SProfesionalAgendaQuery::create()->findPk($request->getParameter('id'));
    $this->forward404Unless($SProfesionalAgenda, sprintf('Object SProfesionalAgenda does not exist (%s).', $request->getParameter('id')));
    $this->form = new SProfesionalAgendaForm($SProfesionalAgenda);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $SProfesionalAgenda = SProfesionalAgendaQuery::create()->findPk($request->getParameter('id'));
    $this->forward404Unless($SProfesionalAgenda, sprintf('Object SProfesionalAgenda does not exist (%s).', $request->getParameter('id')));
    $this->form = new SProfesionalAgendaForm($SProfesionalAgenda);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $SProfesionalAgenda = SProfesionalAgendaQuery::create()->findPk($request->getParameter('id'));
    $this->forward404Unless($SProfesionalAgenda, sprintf('Object SProfesionalAgenda does not exist (%s).', $request->getParameter('id')));
    $SProfesionalAgenda->delete();

    $this->redirect('agenda/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $SProfesionalAgenda = $form->save();

      $this->redirect('agenda/edit?id='.$SProfesionalAgenda->getId());
    }
  }
}
